﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource.Analysis;
using UCL.Powersource.Helpers;
using UCL.Powersource.ViewModels.Analysis;

namespace UCL.Powersource.ViewModels.AnalysisBase
{
    public abstract class TeamComparisonStatisticViewModelBase : StatisticViewModelBase
    {
        public TeamComparisonStatisticViewModelBase()
            : base(StatisticViewModelTypes.MultipleTeams_SingleTimeFrame, false, true, false)
        { }

        protected override void Recalculate_Internal()
        {
            StatisticResultsTable_Recalculate();
        }

        public override bool CanRegisterStatistic(IStatistic statistic)
        {
            return statistic is StatisticCompareTeamsBase;
        }

        protected override bool OnActivating()
        {
            StatisticResultsTable_Recalculate();
            return true;
        }

        protected override bool OnDeactivating()
        {
            return true;
        }

        protected override void OnActiveStatisticChanged()
        {
            base.OnActiveStatisticChanged();
            StatisticResultsTable_Recalculate();
        }

        protected override void OnActiveTimeFrameChanged()
        {
            base.OnActiveTimeFrameChanged();
            StatisticResultsTable_Recalculate();
        }

        protected override void OnTeamProjectConfigurationsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnTeamProjectConfigurationsCollectionChanged(sender, e);
            StatisticResultsTable_Recalculate();
        }

        protected virtual void StatisticResultsTable_Recalculate()
        {
            if (ActiveIReportingContext == null ||
                ActiveIReportingContext.ActiveStatistic == null ||
                ActiveIReportingContext.ActiveTimeFrame == null ||
                ActiveIReportingContext.SelectedITfsTeamProjectConfigurations == null)
            {
                var table = new DataTable();
                StatisticResultsTable = table;
            }
            else
            {
                StatisticResultsTable = DataTableBuilder.GetTableForTeamComparison(ActiveIReportingContext.SelectedITfsTeamProjectConfigurations,
                    ActiveIReportingContext.ActiveTimeFrame, ActiveIReportingContext.ActiveStatistic);
            }
        }
    }
}
